//
//  AHFSingerListViewController.m
//  SlowPlay
//
//  Created by dllo on 15/10/21.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "AHFSingerListViewController.h"
#import "SingerListModel.h"
#import "AHFSIngerListCell.h"

@interface AHFSingerListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) NSMutableArray *singerListArray;/**< 歌手列表数据源*/

@property (nonatomic, strong) UITableView *listTableView; /**< 列表*/

@end


static NSString *const cellIndentifier = @"cellIndentifier";
@implementation AHFSingerListViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.singerListArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createBackButton];
    
    [self getData];
    [self createListTableView];
    
    
    
}




#pragma mark - 数据源
- (void)getData
{
    NSString *urlString = [NSString stringWithFormat:SingerListNetStart@"%@"SingerListNetEnd, _id];
    [NetWork netWorkGETRequestWithURLAndNoMub:urlString Paramater:nil ResultBlock:^(id result) {
        NSDictionary *dic = (NSDictionary *)result;
        NSArray *dataArray = [dic objectForKey:@"data"];
        for (NSDictionary *dic in dataArray) {
            SingerListModel *model = [[SingerListModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.singerListArray addObject:model];
            if (self.singerListArray.count > 0) {
                [self.listTableView reloadData];
            }
        }
    }];
}

#pragma mark - 创建tableView
- (void)createListTableView
{
    self.listTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 128) style:UITableViewStylePlain];
    self.listTableView.delegate = self;
    self.listTableView.dataSource = self;
    [self.listTableView registerClass:[AHFSIngerListCell class] forCellReuseIdentifier:cellIndentifier];
    [self.view addSubview:self.listTableView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.singerListArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SingerListModel *model = [self.singerListArray objectAtIndex:indexPath.row];
    AHFSIngerListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    cell.singerLabel.text = model.singer_name;
    [cell.singerImage sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:[UIImage imageNamed:@"drawerbackpic.jpg"]];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark - 自定义导航栏返回按钮
- (void)createBackButton
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor clearColor];
    button.frame = CGRectMake(10, 10, 40, 40);
    button.layer.cornerRadius = 10;
    button.clipsToBounds = YES;
    [button setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhuianniu.png"] forState:UIControlStateNormal];
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithCustomView:button];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = barButton;
}

- (void)buttonAction:(UIButton *)sender
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
